package com.siki.controller;

import com.siki.dto.PageQueryUserDTO;
import com.siki.dto.SaveDepartmentInfoListDTO;
import com.siki.dto.UserDTO;
import com.siki.config.jsonConfig.JsonResult;
import com.siki.service.UserService;
import com.siki.vo.UserInfoVO;
import com.siki.vo.UserListInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Author Siki
 * @Date 2020/3/2
 */
@RestController
@RequestMapping("/gq_zxxt")
@Api(tags = "报名操作相关接口")
@CrossOrigin //这个注解是为了防止跨域
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/insert")
    @ApiOperation(value = "[提交接口] - 报名信息添加",httpMethod = "POST", consumes = "application/json")
    public JsonResult insertUser(@ApiParam("报名信息DTO") @RequestBody @Validated UserDTO dto) {
        userService.insertUser(dto);
        return JsonResult.success("添加成功");
    }

    @PostMapping("/getList")
    @ApiOperation(value = "[查询接口] - 后台页面 - 查询所有报名者列表",httpMethod = "POST",consumes = "application/json",response = UserListInfoVO.class)
    public JsonResult getList(@ApiParam("条件查询DTO") @RequestBody @Validated PageQueryUserDTO dto){
        return JsonResult.success(userService.PageSelectUserList(dto));
    }

    @GetMapping("/get/{userId}")
    @ApiOperation(value = "[查询接口] - 后台页面 - 根据id查询报名者全部信息",httpMethod = "GET", consumes = "application/json",response = UserInfoVO.class)
    public JsonResult get(@ApiParam("userId") @PathVariable("userId") @NotNull String userId){
        return JsonResult.success(new UserInfoVO(userService.getUser(userId)));
    }

    @PostMapping("/saveUserInfo")
    @ApiOperation(value = "[提交接口] - 后台页面 - 报名者信息更新接口",httpMethod = "POST",consumes = "application/json")
    public JsonResult saveUserInfo(@ApiParam("信息更新DTO") @RequestBody @Validated SaveDepartmentInfoListDTO dto){
        return JsonResult.success(new UserInfoVO(userService.saveDepartmentInfo(dto)));
    }

    @PostMapping("/delete/{userId}")
    @ApiOperation(value = "[删除接口] - 后台页面 - 删除报名信息",httpMethod = "GET",consumes = "application/json")
    public JsonResult delete(@ApiParam("userId")@PathVariable("userId") String id){
        userService.delete(id);
        return JsonResult.success();
    }
}















